package cn.frank.service.mysql.impl;

import cn.frank.vo.TotalEntry;
import cn.frank.domain.mysql.SalesDatas;
import cn.frank.domain.SalesTotal;
import cn.frank.mapper.mysql.SalesDatasMapper;
import cn.frank.service.mysql.ISalesDatasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Frank
 * @date 05-18
 */
@Service
public class ISalesDatasServiceImpl implements ISalesDatasService {
    @Autowired
    private SalesDatasMapper salesDatasMapper;

    @Override
    public Map<String,Object> findSalesDatasList(String statCode) {
        Map<String,Object> data = new HashMap<>();
        //LocalDate currentDate = LocalDate.now();
        //int year = currentDate.getYear();
        //int month = currentDate.getMonthValue() >= 6 ? 12 : 6; // 直接获取当前月份
        SalesDatas s = new SalesDatas();
        s.setStatCode(statCode);
        //s.setSalesYear(year);
        List<SalesDatas> salesDatas = salesDatasMapper.selectSalesDatasList(s);
        LinkedHashSet<String> list = new LinkedHashSet<>();
        List<String> categorys = new ArrayList<>();
        System.out.println(salesDatas);
        List<SalesTotal> totalList = new ArrayList<>();
        if(statCode.trim().length() == 0){
            //给数据分个组
            salesDatas.stream().collect(Collectors.groupingBy(SalesDatas::getSalesType)).forEach((x,y) -> {
                System.out.println(x);
                categorys.add(x);
                List<BigDecimal> temp = new ArrayList<>();
                y.forEach(item->{
                    list.add(item.getSalesMonth().toString());
                    temp.add(item.getSalesValue());
                });
                SalesTotal salesTotal = new SalesTotal(x,temp);
                totalList.add(salesTotal);
            });
            data.put("categorys",categorys);
        }else{
            List<BigDecimal> temp = new ArrayList<>();

            salesDatas.forEach(x -> {
                list.add(x.getSalesDept());
                temp.add(x.getSalesValue());
            });
            SalesTotal salesTotal = new SalesTotal(salesDatas.get(0).getSalesType(),temp);
            totalList.add(salesTotal);
        }

        data.put("xdata", list);
        data.put("result",totalList );

        return Map.of("code","200","data",data,"msg","success");
    }

    @Override
    public Map<String, Object> findDayData() {
        Map<String,Object> map = new HashMap<>();
        List<TotalEntry> list = new ArrayList<>();
        TotalEntry e1 = new TotalEntry("当日原煤调入车数",500+(int) (Math.random()*100));
        TotalEntry e2 = new TotalEntry("当日原煤调入吨数",25000+(int) (Math.random()*1000));
        TotalEntry e3 = new TotalEntry("当日精煤调入车数",500+(int) (Math.random()*100));
        TotalEntry e4 = new TotalEntry("当日精煤调入吨数",500+(int) (Math.random()*100));
        TotalEntry e5 = new TotalEntry("当日精煤上站车数",12000+(int) (Math.random()*1000));
        TotalEntry e6 = new TotalEntry("当日精煤上站吨数",13000+(int) (Math.random()*1000));
        TotalEntry e7 = new TotalEntry("当日中煤上站车数",12000+(int) (Math.random()*1000));
        TotalEntry e8 = new TotalEntry("当日中煤上站吨数",13000+(int) (Math.random()*1000));
        list.addAll(Arrays.asList(e1,e2,e3,e4,e5,e6,e7,e8));

        map.put("data",list);
        map.put("code",200);
        map.put("msg","success");
        return map;
    }

    @Override
    public Map<String, Object> findSalesData() {
        Map<String,Object> map = new HashMap<>();
        List<TotalEntry> list = new ArrayList<>();
        TotalEntry e1 = new TotalEntry("当日精煤销售车数",500+(int) (Math.random()*100));
        TotalEntry e2 = new TotalEntry("当日精煤销量净重",25000+(int) (Math.random()*1000));
        TotalEntry e3 = new TotalEntry("当日中煤销售车数",500+(int) (Math.random()*100));
        TotalEntry e4 = new TotalEntry("当日中煤销量净重",500+(int) (Math.random()*100));
        TotalEntry e5 = new TotalEntry("当日煤泥销售车数",12000+(int) (Math.random()*1000));
        TotalEntry e6 = new TotalEntry("当日煤泥销量净重",13000+(int) (Math.random()*1000));
        list.addAll(Arrays.asList(e1,e2,e3,e4,e5,e6));

        map.put("data",list);
        map.put("code",200);
        map.put("msg","success");
        return map;
    }
}
